################################################################################
# Created By:Pietryka
# Creation Date:  2016-08-22
# Purpose: Matching estimates for the CPS non-voting participation data
# Questions: mpietryka@fsu.edu
################################################################################

# PREAMBLE  ====================================


# LOAD PACKAGES --------------

library(tidyverse)
library(cem)


# LOAD DATA --------------

# DATA FROM 'CPS-1B-Clean-Participation.R'
load("Data/CPS-1B-Clean-Participation.Rdata")



# Coarsened Exact Matching ==========================================



# PRE-MATCHED IMBALANCE  --------------

imbalance(group = cps_sub_10$prekkids,
          data = data.frame(cps_sub_10)[, control_names],
          drop = to_drop)



# SPECIFY CUTS  --------------

age_cuts <- c(18, seq(20, 85, 5))
income_cuts <- c(1, 4, 7, 9, 11, 12, 13)
dummy_cuts <- seq(0, 1, .1)

cps_nonvoting_cuts <- lapply(covariates, function(x){
  x = dummy_cuts
})
names(cps_nonvoting_cuts) <- covariates
cps_nonvoting_cuts$age <- age_cuts
cps_nonvoting_cuts$famincome <- income_cuts



cps_nonvoting_cuts <- list(
  educ = c(31, 39, 40, 43, 46),
  age = c(18, seq(20, 85, 5))
)

cps_nonvoting_groups <- list(
  famincome = c(1:15),
  female = c(0:1),
  student = c(0:1),
  PEMARITL = list(1, c(2:5), 6),
  PTDTRACE = list(1, 2, c(3:6)),
  PEMLR = list(1, 5, c(2:4, 6:7))
)


cps_nonvoting_cem_10 <- cem(treatment = "prekkids",
           #datalist =  impute_10,
           data = data.frame(cps_sub_10),
           drop = outcome_names,
           eval.imbalance = TRUE,
           cutpoints = cps_nonvoting_cuts,
           grouping = cps_nonvoting_groups)



# ESTIMATES =============================================

form_noctrls <- lapply(outcome_names, function(x){
   paste0(x, " ~ ", "prekkids")  %>%
    as.formula()
  })

form_ctrls <- lapply(outcome_names, function(x){
   paste0(x, " ~ ", "prekkids  + age + famincome")  %>%
    as.formula()
  })


cps_nonvoting_est_10 <- map(form_noctrls,
                     ~ att(
                       cps_nonvoting_cem_10,
                       .x,
                       model = "lm",
                       data = data.frame(cps_sub_10))
                     )

cps_nonvoting_est_10



save.image("Data/CPS-4B-Matching-Participation.Rdata")



